
import request from "@/axios"
import store from "@/store"
import common from "@/common/global"
export default ({
    namespaced:true,
    state: {

        // 文章类型
        ArticleTypeData:JSON.parse(localStorage.getItem('ArticleTypeData') )|| [],

        //举报类型
        ReportClassifyData:JSON.parse(localStorage.getItem('ReportClassifyData') )|| [],

        //文章热度榜
        ArticleHotData:JSON.parse(localStorage.getItem('ArticleHotData') ) || null,

        //位置信息
        Location:{},

        //电脑信息
        ComputerInfo:{},

      
        //音乐热度榜
        MusicHotData:JSON.parse(localStorage.getItem('MusicHotData')) || [],

        
        

         
    },
    getters: {
      NewArticleTypeData(state){
         let newData = [{value:-1,label:'全部'}]
         state.ArticleTypeData.forEach(element => {
            let data = {}
            data.value = element.id
            data.label = element.name
            newData.push(data)
         });
         return newData
      }
    },
    mutations: {

    
        //设置文章类型数据
       SETARTICLETYPEDATA:(state,data) =>{
            state.ArticleTypeData=data
            localStorage.setItem('ArticleTypeData',JSON.stringify(data))
       },

      //设置举报类型数据
       SETREPORTCLASSIFYDATA:(state,data) =>{
            state.ReportClassifyData=data
            localStorage.setItem('ReportClassifyData',JSON.stringify(data))
       },

        //设置文章热度数据
       SETARTICLEHOTDATA:(state,data) =>{
          state.ArticleHotData=data
          localStorage.setItem('ArticleHotData',JSON.stringify(data))
       },

       //设置音乐热度榜
        SETMUSICHOTDATA:(state,data) =>{
            state.MusicHotData=data
            localStorage.setItem('MusicHotData',JSON.stringify(data))
         },

        //  设置位置信息
       SETLOCATION:(state,data) => state.Location=data,

       //设置电脑信息
       SETCOMPUTERINFO:(state,data) => state.ComputerInfo ={...state.ComputerInfo,...data}
       
    },
    actions:{
    
      //判断是否登录
     async checkToken(){
        let res=await  request.get("/api/check/token")
        if(res.data.code==-1){
          common.AddInform({type:'danger',content:'Token已失效,请登录'})
          store.dispatch('Login/loginOut')
        } else if(res.data.code== 200) {
            let uid = JSON.parse(localStorage.getItem('User')).uid
            let token=localStorage.getItem('token')
            store.dispatch('Chat/CreateSocket',{uid,token}) //webSorket连接
            store.dispatch('Inform/initInform',uid) //初始化信息
        }
     },

     //初始化博客信息
     initBlog({dispatch,state}){
         //获取文章类型数据
         if(state.ArticleTypeData?.length == 0) dispatch('getArticleTypeData')
         //获取举报类型数据
         if(state.ReportClassifyData?.length== 0) dispatch('getReportClassifyData')
         //获取文章热度榜
         dispatch('getArticleHotData')
          //获取音乐热度榜
          dispatch('getMusicHotData')
     },

    
      //获取举报类型数据
      async  getReportClassifyData(context){
          let res=await  request.get('/api/reportClassify/get')
          if(res.data.code==200){
             context.commit('SETREPORTCLASSIFYDATA',res.data.data)
          }
          return res.data
        },
        //获取文章类型数据
        async  getArticleTypeData(context){
         let res=await  request.get('/api/articleType/get')
         if(res.data.code==200){
            context.commit('SETARTICLETYPEDATA',res.data.data)
         }
         return res.data
       },
      
      //获取文章热度榜
      async  getArticleHotData(context){
        let res=await  request.get('/api/rank/getArticleHot')
        if(res.data.code==200){
           context.commit('SETARTICLEHOTDATA',res.data.data)
        }
        return res.data
      },

       //获取音乐热度榜
       async  getMusicHotData(context){
         let res=await  request.get('/music/search/hot/detail')
         if(res.data.code==200){
            context.commit('SETMUSICHOTDATA',res.data.data)
         }
         return res.data
       }

      //
    }
})

